package cn.jn.lhm.netty.part04;

import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.DefaultPromise;
import io.netty.util.concurrent.Promise;
import lombok.extern.slf4j.Slf4j;
import lombok.val;

/**
 * @author lhm 2753914576@qq.com
 * @time 2023/7/12 - 22:46
 **/
@Slf4j
public class TestPromise {
    @SuppressWarnings("all")
    public static void main(String[] args) {
        val executor = new NioEventLoopGroup(2).next();

        // 可以主动创建promise，结果容器
        Promise<String> promise = new DefaultPromise<>(executor);

        new Thread(() -> {
            log.info("开始计算.....");
            try {
                Thread.sleep(2000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            //promise.setSuccess("aaa");
            promise.setFailure(new RuntimeException("111"));
        }, "A").start();

        // 主线程获取对应的结果
        promise.addListener(future -> {
            log.info("获取结果:{}", future.get());
            executor.shutdownGracefully();
        });
    }
}
